<!-- Web Upload Search Result PHP File -->
<!-- Reveions History ...
	Rev 1.00 at 2015/06/08 by Max Yin:
		1. First time create
	Rev 1.01 at 2015/07/08 by Max Yin:
		1. 由標楷體, 改成新細明體
		2. Upload 的 WSID 增加重复多站选择
		3. 增加 Discrption 搜寻
-->
<html>
	<body background="pic/background_06.gif">
	<head>
		<font face="新細明體,time new roman,Arial">
		<meta charset="UTF-8">
	</head>
	<body>
		<center>
			<font size="5" color="#0000FF"><b>--- 程式,查询结果 ---</b></font><br>
			<hr>
			<?php
				require_once('class_package/common.php');
				// $partnum = CRL615766000A,CRL615766007A,...
				$history_username	= strtoupper($_POST['history_username']);
				setcookie('source_name',$history_username);
				$partnum			= strtoupper($_POST['partnum']);
				setcookie('source_pn',$partnum);
				if ( strlen($history_username) > 30 ) {
					echo "<script>alert('输入的-User Upload History(用戶上傳歷史)不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($partnum) > 13 ) {
					echo "<script>alert('输入的-Part Number(产品料号)不能为空值或超过13个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				
				// To search all table ...
				$db_select      = Sql_Download::sql_db;
				$db_table       = Sql_Download::sql_table;
				$table_name1    = Sql_Download::table_name1;	// 'webdl_pn';
				$table_name8	= Sql_Download::table_name8;	// 'webdl_create_date';
				$table_name9	= Sql_Download::table_name9;	// 'webdl_creator';
				
				// To delete over 2 years columns..
				$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
				if ( mysqli_connect_errno($conn) ) { 
					echo "<script>alert('MySQL资料库连接错误, 请联系统开发人员!!');history.go(-1);</script>";
					exit();
				}
				
				mysqli_set_charset($conn,'set names utf8');
				$sqli_cmd = "SELECT * FROM `$db_table` WHERE `$table_name9`='$history_username' ORDER BY (`$table_name8`) DESC";
				$res = mysqli_query($conn,$sqli_cmd);	// 透过 execute_query 对象实例完成查询
				if ( !$res ) { 
					mysqli_close($conn);				// 关闭资料库
					echo "<script>alert('MySQL资料库查询错误, 请联系统开发人员!!');history.go(-1);</script>";
					exit();
				}
				
				$row = mysqli_num_rows($res);		// 返回结果集中栏位的数量。
				if ( $row > 0 ) {														
					while ( $getArrays = mysqli_fetch_array($res,MYSQLI_ASSOC) ) {					
						$put_pn          = $getArrays['webdl_pn'];
						$put_wsid        = $getArrays['webdl_wsid'];
						$put_tpver       = $getArrays['webdl_tpver'];
						$put_work_order  = $getArrays['webdl_work_order'];
						$put_line        = $getArrays['webdl_line'];
						$put_panel       = $getArrays['webdl_panel'];
						$put_create_date = $getArrays['webdl_create_date'];
						$put_creator     = $getArrays['webdl_creator'];
						$put_description = $getArrays['webdl_description'];
						$put_ftpip       = $getArrays['webdl_ftpip'];
						$d1=strtotime(date('Y-m-d G:i:s')); 
						$d2=strtotime($put_create_date);
						$d3=$d1-$d2;
						$Days=round(($d1-$d2)/3600/24);					
						/* 1090  3*/	
						/* 730  2*/	
						/* 1460  4*/	
			
						// if ( $Days > 730 ) {	
							// if ( is_file($put_ftpip) ) unlink($put_ftpip);
							// download_delete($put_pn,$put_wsid,$put_tpver,$put_work_order,$put_line,$put_panel);
						// }
					}
				}
				mysqli_close($conn);	// 关闭资料库
				
				// To search all table ...
				$err_chk        = '';
				$WSID_chkCnt	= 1;
				$show_num		= 0;	
				$conn			= '';
				$sql_cmd		= '';
				$rows			= 0;
				$result			= '';
				$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
				if ( mysqli_connect_errno($conn) ) { 
					echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
					exit();
				}
				
				mysqli_set_charset($conn,'set names utf8');
				if (1) {
					$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1` LIKE '%$partnum%' AND `$table_name9` LIKE '%$history_username%' ORDER BY (`$table_name8`) DESC";
				} else {
					if ( strlen($history_username) > 0 && strlen($partnum) == 0 ) {
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$partnum'";
					} elseif ( strlen($history_username) == 0 && strlen($partnum) > 0 ) {
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name9`='$history_username'";
					} else {
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$partnum' AND `$table_name9`='$history_username'";
					}
				}
				$result = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
				if ( !$result ) { 
					mysqli_close($conn);				// 关闭资料库
					echo "<script>alert('MySql资料库搜寻失败,请跟开发人员查明原因...');history.go(-1);</script>";
					exit();
				}
				
				$rows = mysqli_num_rows($result);		// 返回结果集中栏位的数量。
				if ( $rows == 0 ) { 
					echo "<script>alert('WSID(站別) 栏位中, 有某些站位查询不到, 请重新建立...');history.go(-1);</script>";
					exit();
				}
				
				echo '<br>
				<table border="2" width="75%">
					<tr bgcolor="#70DB93">
						<th>No.</th>
						<th>工單</th>
						<th>Part Number</th>
						<th>WSID</th>
						<th>TPVER</th>
						<th>线别</th>
						<th>制程别</th>
						<th>Create Date/Time</th>
						<th>Create By</th>
						<th>Description(位址/ChkSum)</th>
						<th>Download</th>
						<th>Delete</th>
						<th>Update</th>
					</tr>';
				
				while ( $getArray = mysqli_fetch_array($result,MYSQLI_ASSOC) ) {
					$show_num++;
					$regex = '/\/COMMON\//i';
					$put_pn          = $getArray['webdl_pn'];
					$put_wsid        = $getArray['webdl_wsid'];
					$put_tpver       = $getArray['webdl_tpver'];
					$put_work_order  = $getArray['webdl_work_order'];
					$put_line        = $getArray['webdl_line'];
					$put_panel       = $getArray['webdl_panel'];
					$put_create_date = $getArray['webdl_create_date'];
					$put_creator     = $getArray['webdl_creator'];
					$put_description = $getArray['webdl_description'];
					$put_ftpip       = $getArray['webdl_ftpip'];
					$webul_update = "webul_download_update_a.php?creator=$put_creator";
					$webul_delete	 = "";
					if ( strpos($put_tpver,' ') > -1 ) {
						$webul_delete = "webul_download_delete_a.php?creator=$put_creator";	
					} else {
						if(preg_match($regex, $put_ftpip)){
							$webul_delete = "webul_download_delete_a.php?creator=$put_creator";
						
						} else {
							$webul_delete = "webul_download_delete_c.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&creator=$put_creator&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip";
						}
					}
					
					echo "
						<tr>
							<th>$show_num</th>
							<th><font size='4' color='#0000FF'><b>$put_work_order</b></font></th>
							<th>$put_pn</th>
							<th>$put_wsid</th>
							<th>$put_tpver</th>
							<th>$put_line</th>
							<th>$put_panel</th>
							<th>$put_create_date</th>
							<th>$put_creator</th>
							<th>$put_description</th>
							<th><a href=$put_ftpip>下 载</a></th>
							<th><a href=$webul_delete>删 除</a></th>
							<th><a href=$webul_update>修 改</a></th>
						</tr>";
				}
				echo '</table><br>';
				mysqli_free_result($result);			// 释放 MySQL $result
				mysqli_close($conn);					// 关闭资料库						
			?>
		</center>
	</body>
</html>

